package net.zdreamer.geracomium.ui.more;

import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.widget.ListView;

import com.example.geracomium.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;

import net.zdreamer.geracomium.adapter.GeracomiumListAdapter;
import net.zdreamer.geracomium.entity.GeracomiumInfos;
import net.zdreamer.geracomium.entity.GeracomiumReserveInfos;
import net.zdreamer.geracomium.ui.BaseActivity;

public class ReserveBedActivity extends BaseActivity {
	private GeracomiumListAdapter mAdapter;
	private GeracomiumInfos geracomiumInfos;
	private PullToRefreshListView reservebed;

	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.activity_reservebed;
	}

	@Override
	protected void init() {
		initTitle("床位预约", R.drawable.selector_title_back, 0, true, false);
		reservebed = (PullToRefreshListView) findViewById(R.id.reservebed);
	}

	@Override
	protected void initViews() {
		GeracomiumInfos infos = new GeracomiumInfos();
		geracomiumInfos = infos;
		reservebed.setMode(Mode.BOTH);
		reservebed.setOnRefreshListener(orl);
		mAdapter = new GeracomiumListAdapter(this,
				geracomiumInfos.geracomiumInfos,
				R.layout.list_geracomium_detail);
		reservebed.getRefreshableView().setAdapter(mAdapter);

	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub

	}
	com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2<ListView> orl = new OnRefreshListener2<ListView>() {

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			// page = 0;
			// onLoadDatas();
			String label = DateUtils.formatDateTime(ReserveBedActivity.this
					, System.currentTimeMillis(),
					DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
							| DateUtils.FORMAT_ABBREV_ALL);

			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(
					"上次刷新：" + label);

			Thread a = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					Message message = new Message();
					message.what = 0;
					h.sendMessage(message);
				}
			});
			a.start();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			// page++;
			// onLoadDatas();
			String label = DateUtils.formatDateTime(ReserveBedActivity.this, System.currentTimeMillis(),
					DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
							| DateUtils.FORMAT_ABBREV_ALL);

			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(
					"上次刷新：" + label);

			Thread a = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					Message message = new Message();
					message.what = 0;
					h.sendMessage(message);
				}
			});
			a.start();
		}
	};

	Handler h = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				GeracomiumReserveInfos infos = new GeracomiumReserveInfos();
				mAdapter = new GeracomiumListAdapter(ReserveBedActivity.this,
						geracomiumInfos.geracomiumInfos,
						R.layout.list_geracomium_detail);
				reservebed.setAdapter(mAdapter);
				reservebed.onRefreshComplete();
			}
		}
	};
	
}
